package com.hhit.collaborative_learning.communication;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.CoopDao;
import com.hhit.collaborative_learning.dao.CoopTaskDao;
import com.hhit.collaborative_learning.dao.impl.CoopDaoImpl;
import com.hhit.collaborative_learning.dao.impl.CoopTaskDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.JsonUtil;
import com.hhit.collaborative_learning.utils.ResponseUtil;
import com.mysql.jdbc.StringUtils;

/**
 * Servlet implementation class GetCoopStatisticsServlet
 */
@WebServlet("/communication/GetCoopStatisticsServlet")
public class GetCoopStatisticsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetCoopStatisticsServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String projID = request.getParameter("projID");
		String team_id = request.getParameter("team_id");
		Timestamp newTime = new Timestamp(System.currentTimeMillis());
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String ntime = sdf.format(newTime);
		int taskNum = 0, nodoneNum = 0;
		double delayRate = 0.0;
		JSONObject jobj = new JSONObject();
		JSONArray taskNumArray = new JSONArray();
		JSONArray noDoneNumArray = new JSONArray();
		Connection con = DBAccess.getInstance().getConn();

		CoopTaskDao td = new CoopTaskDaoImpl();
		CoopDao cd = new CoopDaoImpl();

		if (!StringUtils.isNullOrEmpty(projID)) {
			taskNum = td.getCount(con, " pid=" + projID);
			nodoneNum = td.getCount(con, " pid=" + projID + " and etime < '"
					+ newTime.toString() + "'");
			if (taskNum != 0) {
				delayRate = (double) (((double) nodoneNum) / ((double) taskNum));
				delayRate = delayRate * 100;
				DecimalFormat df = new DecimalFormat("0.0");
				delayRate = Double.parseDouble(df.format(delayRate));
			}

		}

		if (!StringUtils.isNullOrEmpty(team_id)) {
			ResultSet rTasknum = td.getUserTaskCount(con, " pid=" + projID);
			ResultSet rNodone = td.getUserTaskCount(con, " pid=" + projID
					+ " and etime < '" + newTime.toString() + "'");
			try {
				taskNumArray = JsonUtil.formatRsToJsonArray(rTasknum);
				noDoneNumArray = JsonUtil.formatRsToJsonArray(rNodone);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		jobj.put("newTime", ntime);
		jobj.put("taskNum", taskNum);
		jobj.put("nodoneNum", nodoneNum);
		jobj.put("delayRate", delayRate);
		jobj.put("taskNumArray", taskNumArray);
		jobj.put("noDoneNumArray", noDoneNumArray);
		try {
			ResponseUtil.write(response, jobj.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBAccess.getInstance().close(con, null, null);
			System.out.println("statistics json " + jobj.toString());
		}

	}

}
